Page({
  data: {
    list: [],
    checkValue: [],
    currentIndex: 2,
    handOrderNum: "",
    backgroundColor: "#739FF8",
    selectAllChecked: false,
    processName: "",
    isShowCancel: false,
  },
  getList(handOrderNum, index) {
    console.log(handOrderNum);
    wx.showLoading({
      title: '加载中',
    })
    global.$api.post({
      url: '/api/skirt/getFinePrecessAppletOutList',
      data: {
        handOrderNum: handOrderNum,
        type: index,
        processName: this.data.processName,
        page: 1,
        limit: 999,
      }
    }).then(res => {
      console.log(res);
      let list = res.data.map(val => {
        return {
          ...val,
          checked: false,
        }
      })
      this.setData({
        list: list,
        isShowCancel: list.length != 0 ? list[0].status == 5 ? true : false : false
      })
      console.log(this.list);
    }).finally(() => {
      wx.hideLoading();
    })
  },
  //跳转二级页面
  checkboxItemClick(e) {
    console.log(1, e);
    wx.navigateTo({
      url: `/pages/index/checknameDetails/checknameDetails?newProcessId=${e.target.dataset.index.newProcessId}&handOrderNum=${this.data.handOrderNum}&type=${this.data.currentIndex}`,
      fail: function (err) {
        console.log(err);
      }
    })
  },
  toggleSelectAll(e) {
    console.log(e);
    if (e.detail.value.length == 0) {
      let list = this.data.list.map(val => {
        return {
          ...val,
          checked: false
        }
      })
      this.setData({
        list: list
      })
      this.setData({
        checkValue: [],
      })
    } else {
      let list = this.data.list.map(val => {
        return {
          ...val,
          checked: true
        }
      })
      let value = this.data.list.map(val => val.newProcessId);
      this.setData({
        list: list
      })
      this.setData({
        checkValue: value
      })
    }
  },
  handleTabChange: function (type) {
    console.log(type);
    let index = type.currentTarget.dataset.index;
    this.setData({
      checkValue: [],
      selectAllChecked: false,
      currentIndex: index,
      processName: "",
    })
    if (index != 3) {
      this.getList(this.data.handOrderNum, index)
    } else {
      this.getproList(this.data.handOrderNum);
    }
  },
  //生产记录
  getproList(handOrderNum) {
    wx.showLoading({
      title: '加载中',
    })
    global.$api.post({
      url: 'api/skirt/getFinePrecessAppletOutCompleteList',
      data: {
        handOrderNum: handOrderNum,
        processName: this.data.processName,
        page: 1,
        limit: 999,
      }
    }).then(res => {
      let list = res.data;
      this.setData({
        list: list
      })
    }).finally(() => {
      wx.hideLoading();
    })
  },
  handleClose(e) {
    console.log(e);
    global.$api.post({
      url: "api/skirt/getReleaseFinePrecessOut",
      data: {
        handOrderNum: this.data.handOrderNum,
        type: this.data.currentIndex,
        newProcessIds: e.currentTarget.dataset.index
      }
    }).then(res => {
      wx.showToast({
        title: '操作成功！',
        icon: 'none'
      })
      this.getList(this.data.handOrderNum, this.data.currentIndex)
    })
  },
  handleCancel() {
    let that = this;
    wx.showModal({
      title: "提示",
      content: "确定要取消吗？",
      success() {
        console.log("确定");
        wx.showLoading({
          title: '加载中',
          mask: true,
        })
        global.$api.post({
          url: '/api/skirt/canceling',
          data: {
            handOrderNum: that.data.handOrderNum,
          }
        }).then(res => {
          wx.showToast({
            title: '操作成功！',
            icon: 'none'
          })
          wx.hideLoading();
          that.getList(that.data.handOrderNum, that.data.currentIndex)
        })
      }
    })
  },
  handleSubmit() {
    console.log(1, this.data.checkValue);
    if (this.data.checkValue.length == 0) {
      wx.showToast({
        title: '请先选择数据！',
        icon: 'none'
      })
      return
    }
    global.$api.post({
      url: 'api/skirt/accomplishFineApplet',
      data: {
        handOrderNum: this.data.handOrderNum,
        type: this.data.currentIndex,
        newProcessIds: this.data.checkValue.join(",")
      }
    }).then(res => {
      wx.showToast({
        title: '操作成功！',
        icon: 'none'
      })
      this.getList(this.data.handOrderNum, this.data.currentIndex)
    })
  },
  inputBlur(e) {
    this.setData({
      processName: e.detail.value
    })
    if (this.data.currentIndex == 1 || this.data.currentIndex == 2) {
      this.getList(this.data.handOrderNum, this.data.currentIndex)
    } else {
      this.getproList(this.data.handOrderNum)
    }
    console.log(e);
  },
  checkboxChange(e) {
    console.log(2, e);
    if (e.detail.value.length != this.data.list.length) {
      this.setData({
        selectAllChecked: false,
      })
    } else {
      this.setData({
        selectAllChecked: true,
      })
    }
    this.setData({
      checkValue: e.detail.value
    })
  },
  onLoad: function (options) {
    console.log("options.handOrderNum", options.handOrderNum);
    this.setData({
      handOrderNum: options.handOrderNum
    }, function () {
      this.getList(options.handOrderNum, 2)
    })
  }
})